<?php
class IconSmsKuteController extends CpController
{
    public function actionCategory()
    {
        $currentPage = isset($_GET["page"]) ? intval($_GET["page"]) : 1;
        $currentPage = $currentPage==0 ? 1 : $currentPage;
        $numberRecordPerPage = Yii::app()->params["numberRecordPerPage"];
        
        $category = AIconSmsKute::GetAllCategory($currentPage, $numberRecordPerPage);
        $total_category = AIconSmsKute::GetTotalCategory();
        $numberPage = ceil($total_category / $numberRecordPerPage);
        
        $this->render(
            "category"
            , array(
                "category"=>$category
                , "total_category"=>$total_category       
                , "currentPage"=>$currentPage       
                , "numberPage"=>$numberPage       
            )
        );   
    }    
    
    public function actionAddCategory()
    {
        $error = "";
        $data = array();
        
        if(isset($_POST["title"])){
            $data["title"] = trim(strip_tags($_POST["title"]));
            $data["position"] = intval($_POST["position"]);
            
            $error .= empty($data["title"]) ? "<p>- Bạn chưa nhập tên danh mục</p>" : "";
            if(empty($error)){
                $data["status"] = 1;
                $data["create_date"] = time();
                
                $categoryId = AIconSmsKute::InsertCategory($data);
                if($categoryId>0){
                    $act = 1;
                    $obj = 22;
                    $content = LogConfig::$action[$act] . ' ' . LogConfig::$object[$obj] . ': {title: ' . $data["title"];
                    $content .= ',position: ' . $data["position"] . ',status: ' . $data["status"] . ',id: ' . $categoryId . '}';
                    $result = AAdminLog::insertAdminLog($act, $obj, $content);                    
                    
                    $this->redirect(Url::createUrl("iconSmsKute/category"));
                }
            }
        }
        
        $this->render(
            "category_add"
            , array(
                "error"=>$error
                , "data"=>$data
            )
        );   
    }
    
    public function actionEditCategory()
    {
        $id = isset($_GET["id"]) ? intval($_GET["id"]) : 0;
        $data = AIconSmsKute::GetCategoryInfo($id);
        if(empty($data)){
            throw new CHttpException(404, "Trang yêu cầu không tồn tại");
        }
        $error = "";        
        
        if(isset($_POST["title"])){
            $data_edit = array();
            $data_edit["title"] = trim(strip_tags($_POST["title"]));
            $data_edit["position"] = intval($_POST["position"]);
            
            $error .= empty($data_edit["title"]) ? "<p>- Bạn chưa nhập tên danh mục</p>" : "";
            if(empty($error)){                                
                $data_edit["id"] = $id;
                $categoryId = AIconSmsKute::UpdateCategory($data_edit);
                if($categoryId>0){
                    $act = 2;
                    $obj = 22;
                    $content = LogConfig::$action[$act] . ' ' . LogConfig::$object[$obj] . ': {title: ' . $data["title"] . ", position: " . $data["position"] . "} Thành ";
                    $content .= '{title: ' . $data_edit["title"] . ', position: ' . $data_edit["position"] . ', id: ' . $id . '}';
                    $result = AAdminLog::insertAdminLog($act, $obj, $content);                    
                    
                    $this->redirect(Url::createUrl("iconSmsKute/category"));
                }
            }
        }
        
        $this->render(
            "category_edit"
            , array(
                "error"=>$error
                , "data"=>$data
            )
        );   
    }
    
    public function actionAjaxQuickUpdateCategory()
    {        
        $data["status"] = isset($_POST["status"]) ? intval($_POST["status"]) : 0;        
        $data["position"] = isset($_POST["position"]) ? intval($_POST["position"]) : 0;        
        $data["id"] = isset($_POST["id"]) ? intval($_POST["id"]) : 0;
        
        if($data["id"] >0){   
            $result = AIconSmsKute::QuickUpdateCategory($data);
            
            if($result>0){
                $act = 2;
                $obj = 22;
                $content = LogConfig::$action[$act] . ' ' . LogConfig::$object[$obj] . ': {IconCategoryId: ' . $data["id"] . '} thành';
                $content .= '{IconCategoryId: ' . $data["id"] . ',status: ' . $data["status"] . ',position: ' . $data["position"] . '}';
                $result = AAdminLog::insertAdminLog($act, $obj, $content);                  
                echo 1;exit();
            }else{
                echo "Chưa thay đổi dữ liệu";
            }
        }
    }
    
    public function actionAjaxDeleteCategory()
    {        
        $id = isset($_POST["id"]) ? intval($_POST["id"]) : 0;
        if($id >0){
            $result = AIconSmsKute::DeleteCategory($id);
            if($result >0){
                $act = 3;
                $obj = 22;
                $content = LogConfig::$action[$act] . " " . LogConfig::$object[$obj] . ": {IconCategoryId: " . $id . "}";
                $result = AAdminLog::insertAdminLog($act, $obj, $content);                  
                echo 1;exit();
            } else {
                echo "Chưa thay đổi dữ liệu";
            }
        }
    } 
    
    public function actionIndex()
    {
        $category = AIconSmsKute::GetCategory();
        
        $currentPage = isset($_GET["page"]) ? intval($_GET["page"]) : 1;
        $currentPage = $currentPage==0 ? 1 : $currentPage;
        $numberRecordPerPage = Yii::app()->params["numberRecordPerPage"];
        
        $search = array();
        $search["categoryId"] = isset($_GET["categoryId"]) ? intval($_GET["categoryId"]) : 0;
        $search["status"] = isset($_GET["status"]) ? Common::cleanQuery($_GET["status"]) : "";
        $icon = AIconSmsKute::GetAllIcon($search, $currentPage, $numberRecordPerPage);
        $total_icon = AIconSmsKute::GetTotalIcon($search);
        $numberPage = ceil($total_icon / $numberRecordPerPage);
        
        $this->render(
            "index"
            , array(
                "icon"=>$icon
                , "total_icon"=>$total_icon       
                , "currentPage"=>$currentPage       
                , "numberPage"=>$numberPage       
                , "category"=>$category       
                , "search"=>$search       
            )
        );
    }
    
    public function actionAdd()
    {
        $category = AIconSmsKute::GetCategory();
        $error = "";
        $data = array();
        
        $time = time();
        $location = getcwd() . "/upload/icon_app_sms_kute/";        
        $location .= date("Y/md/", $time);
        
        if(isset($_POST["categoryId"])){
            $file = $_FILES["filename"];            
            $tmp_name = $file["tmp_name"];
            
            $data["categoryId"] = intval($_POST["categoryId"]);
            $data["picture"] = $file["name"];
            $data["position"] = intval($_POST["position"]);
            
            $error .= empty($data["picture"]) ? "<p>- Bạn chưa chọn icon</p>" : "";
            if(empty($error)){
                if(@chdir($location) == false) @mkdir($location, 0777, true);
                if(@move_uploaded_file($tmp_name, $location . $data["picture"])){
                    $data["status"] = 1;
                    $data["create_date"] = $time;
                    
                    $iconId = AIconSmsKute::InsertIcon($data);
                    if($iconId>0){
                        $act = 1;
                        $obj = 23;
                        $content = LogConfig::$action[$act] . ' ' . LogConfig::$object[$obj] . ': {categoryId: ' . $data["categoryId"];
                        $content .= ',picture: ' . $data["picture"] .  ',position: ' . $data["position"] . ',status: ' . $data["status"] . ',id: ' . $iconId . '}';
                        $result = AAdminLog::insertAdminLog($act, $obj, $content);                    
                        
                        $this->redirect(Url::createUrl("iconSmsKute/index"));
                    }    
                }
                
            }
        }
        
        $this->render(
            "add"
            , array(
                "error"=>$error
                , "data"=>$data
                , "category"=>$category
            )
        );   
    }
    
    public function actionEdit()
    {
        $category = AIconSmsKute::GetCategory();
        
        $id = isset($_GET["id"]) ? intval($_GET["id"]) : 0;
        $data = AIconSmsKute::GetIconInfo($id);
        if(empty($data)){
            throw new CHttpException(404, "Trang yêu cầu không tồn tại");
        }
        
        $error = "";        
        
        if(isset($_POST["categoryId"])){            
            $data_edit = array();
            $data_edit["categoryId"] = intval($_POST["categoryId"]);            
            $data_edit["position"] = intval($_POST["position"]);
                        
            if(empty($error)){                                    
                $data_edit["id"] = $id;    
                $result = AIconSmsKute::UpdateIcon($data_edit);
                if($result>=0){
                    $act = 2;
                    $obj = 23;
                    $content = LogConfig::$action[$act] . ' ' . LogConfig::$object[$obj] . ": {position: " . $data["position"] . ", categoryId: " . $data["categoryId"] . "} Thành ";
                    $content .= '{position: ' . $data_edit["position"] . ', categoryId: ' . $data_edit["categoryId"] . ', id: ' . $id . '}';
                    $result = AAdminLog::insertAdminLog($act, $obj, $content);                    
                    
                    $this->redirect(Url::createUrl("iconSmsKute/index"));
                }                
            }
        }
        
        $this->render(
            "edit"
            , array(
                "error"=>$error
                , "data"=>$data
                , "category"=>$category
            )
        );   
    }
    
    public function actionAjaxQuickUpdate()
    {        
        $data["status"] = isset($_POST["status"]) ? intval($_POST["status"]) : 0;        
        $data["isHot"] = isset($_POST["isHot"]) ? intval($_POST["isHot"]) : 0;        
        $data["date_of_hot"] = $data["isHot"]==0 ? 0 : time();
        $data["position"] = isset($_POST["position"]) ? intval($_POST["position"]) : 0;        
        $data["id"] = isset($_POST["id"]) ? intval($_POST["id"]) : 0;
        
        if($data["id"] >0){   
            $result = AIconSmsKute::QuickUpdateIcon($data);
            
            if($result>0){
                $act = 2;
                $obj = 23;
                $content = LogConfig::$action[$act] . ' ' . LogConfig::$object[$obj] . ': {iconId: ' . $data["id"] . '} thành';
                $content .= '{iconId: ' . $data["id"] . ',isHot: ' . $data["isHot"] . ',status: ' . $data["status"] . ',position: ' . $data["position"] . '}';
                $result = AAdminLog::insertAdminLog($act, $obj, $content);                  
                echo 1;exit();
            }else{
                echo "Chưa thay đổi dữ liệu";
            }
        }
    }
    
    public function actionAjaxDelete()
    {        
        $id = isset($_POST["id"]) ? intval($_POST["id"]) : 0;
        if($id >0){
            $result = AIconSmsKute::DeleteIcon($id);
            if($result >0){
                $act = 3;
                $obj = 23;
                $content = LogConfig::$action[$act] . " " . LogConfig::$object[$obj] . ": {IconId: " . $id . "}";
                $result = AAdminLog::insertAdminLog($act, $obj, $content);                  
                echo 1;exit();
            } else {
                echo "Chưa thay đổi dữ liệu";
            }
        }
    }
}
?>
